<?php
    class Marque {
        //Alias sur table : ma
        public $id_marque;
        public $libelle;
        public $email;
        public $tel;
        public $image;
        public $date_ajout;
        public $date_modif;
        public $valide;

        //Constructeur complet
        public function __construct($id_marque, $libelle, $email, $tel, $image, $date_ajout, $date_modif, $valide) {
            $this->setId($id_marque);
            $this->setLibelle($libelle);
            $this->setEmail($email);
            $this->setTel($tel);
            $this->setImage($image);
            $this->setDateAjout($date_ajout);
            $this->setDateModif($date_modif);
            $this->setValide($valide);
        }
		
        //Retourne le nom de la marque
        //Paramètre : aucun
        //Retourne (String)
        public function __toString() {
            return $this->libelle;
        }
        
        //Ajoute un objet Marque à la BDD
        //Paramètre : aucun
        //Retourne (Marque)
        public function add() {
            $sql =  'INSERT INTO marque (libelle, email, tel, image, date_ajout, date_modif, valide) VALUES
                    ("'.Db::insertion($this->libelle).'", "'.Db::insertion($this->email).'", "'.Db::insertion($this->tel).'", 
                    "'.Db::insertion($this->image).'", "'.$this->getDateAjout().'", "'.$this->getDateModif().'", '.intval($this->valide).')';
            $this->setId(Db::Execute($sql));
            return $this;
        }
        
        //Modifie un objet Marque
        //Paramètre : aucun
        //Retourne (Marque)
        public function update() {
            $this->setDateModif(new DateTime());
            $sql =  'UPDATE marque
                    SET libelle = "'.Db::insertion($this->libelle).'", 
                        email = "'.Db::insertion($this->email).'", 
                        tel = "'.Db::insertion($this->tel).'", 
                        image = "'.Db::insertion($this->image).'", 
                        date_modif = "'.$this->getDateModif().'",
                        valide = '.intval($this->valide) .'
                    WHERE
                        id_marque = '.intval($this->id_marque);
            Db::Execute($sql);
            return $this;
        }
        
        //Désactive une marque
        //Paramètre : aucun
        //Retourne (Marque) 
        public function suppr() {
            $this->setValide(false);
            return $this->update();
        }
        
        //Rcupère toutes les marques de la BDD
        //Paramètre : (Booléen)valide
        //Retourne collection de Marque
        public static function getMarques($valide = true){
            $sql =  'SELECT
                        ma.id_marque as maid_marque, ma.libelle as malibelle, ma.email as maemail, ma.tel as matel, ma.image as maimage, 
                        ma.date_ajout as madate_ajout, ma.date_modif as madate_modif, ma.valide as mavalide
                    FROM marque ma
                    WHERE
                        ma.valide = '.intval($valide).'
                    ORDER BY ma.libelle';
            $ps = Db::Execute($sql, true);
            $tabRetour = array();
            foreach($ps as $p) {
                $tabRetour[] = new Marque($p['maid_marque'], $p['malibelle'], $p['maemail'], $p['matel'], $p['maimage'], $p['madate_ajout'], 
                                    $p['madate_modif'], $p['mavalide']);
            }
            return $tabRetour;
        }
        
        //Crée un objet Marque grâce à son ID
        //Paramètre : (int)id
        //Retourne (Marque)
        public static function rechercheMarque($id) {
            $sql =  'SELECT
                        ma.id_marque as maid_marque, ma.libelle as malibelle, ma.email as maemail, ma.tel as matel, ma.image as maimage, 
                        ma.date_ajout as madate_ajout, ma.date_modif as madate_modif, ma.valide as mavalide
                    FROM marque ma
                    WHERE
                        ma.id_marque = '.intval($id);
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                return new Marque($p['maid_marque'], $p['malibelle'], $p['maemail'], $p['matel'], $p['maimage'], $p['madate_ajout'], 
                                    $p['madate_modif'], $p['mavalide']);
            }
            return null;
        }
        
        //GETTERS ET SETTERS
        public function getId() {return $this->id_marque;}
        public function getLibelle() {return $this->libelle;}
        public function getEmail() {return $this->email;}
        public function getTel() {return Tools::getterPhone($this->tel);}
        public function getImage() {return $this->image;}
        public function getDateAjout($format = "Y-m-d H:i:s") {return Tools::getterDate($this->date_ajout, $format);}
        public function getDateModif($format = "Y-m-d H:i:s") {return Tools::getterDate($this->date_modif, $format);}
        public function getValide() {return $this->valide;}

        public function setId($temp) {$this->id_marque = intval($temp);}
        public function setLibelle($temp) {$this->libelle = $temp;}
        public function setEmail($temp) {$this->email = $temp;}
        public function setTel($temp) {$this->tel = Tools::setterPhone($temp);}
        public function setImage($temp) {$this->image = $temp;}
        public function setDateAjout($temp) {$this->date_ajout = Tools::setterDate($temp);}
        public function setDateModif($temp) {$this->date_modif = Tools::setterDate($temp);}
        public function setValide($temp) {$this->valide = intval($temp);}
    }

?>